package com.yami.shop.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.app.param.VipGiftRecordEditParam;
import com.yami.shop.bean.app.param.VipGiftRecordParam;
import com.yami.shop.common.util.PageParam;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.yami.shop.service.VipGiftRecordService;
import com.yami.shop.bean.model.VipGiftRecord;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 赠送vip套餐 前端控制器
 * </p>
 *
 * @author chajiu
 * @since 2021-10-07
 */

@Api(tags = "赠送vip接口")
@RestController
@RequestMapping("/vip-gift-record")
public class VipGiftRecordController {


    @Autowired
    private VipGiftRecordService VipGiftRecordService;

    @Autowired
    private  MapperFacade mapperFacade;

    /**
     * 分页获取
    */

    @ApiOperation(value = "查询赠送记录", notes = "可以查询指定user和领取状态的记录")
     @GetMapping("/page")
     @PreAuthorize("@pms.hasPermission('admin:VipGiftRecord:page')")
     public ResponseEntity<IPage<VipGiftRecord>> page(VipGiftRecord entity,PageParam<VipGiftRecord> page) {
         IPage<VipGiftRecord> iPage = VipGiftRecordService.page(page, new LambdaQueryWrapper<VipGiftRecord>()
         .eq(entity.getUserId()!=null,VipGiftRecord::getUserId,entity.getUserId())
         .eq(entity.getStatus()!=null,VipGiftRecord::getStatus,entity.getStatus()));
         return ResponseEntity.ok(iPage);
     }

    /**
     * 保存
     */
    @ApiOperation(value = "赠送vip", notes = "可赠送多个用户，逗号拼接uid,如：'123,124,125'")
    @PostMapping("/save")
    @PreAuthorize("@pms.hasPermission('admin:VipGiftRecord:save')")
    public ResponseEntity<Void> save(@Valid @RequestBody VipGiftRecordParam entity) {
        List<String> ids = Arrays.asList(entity.getUserIds().split(","));
        if(ids.size()==0){
            throw new RuntimeException("id不能为空");
        }
        for (String id : ids) {
            VipGiftRecord record = mapperFacade.map(entity,VipGiftRecord.class);
            record.setStatus(0);
            record.setCreateTime(new Date());
            record.setUserId(id);
            VipGiftRecordService.save(record);
        }
        return ResponseEntity.ok().build();
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@pms.hasPermission('admin:VipGiftRecord:info')")
    public ResponseEntity<VipGiftRecord> info(@PathVariable("id") String id){
        VipGiftRecord entity = VipGiftRecordService.getById(id);
            return ResponseEntity.ok(entity);
    }

    /**
     * 修改
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin:VipGiftRecord:update')")
    public ResponseEntity<Void> update(@Valid @RequestBody VipGiftRecordEditParam entity){
        VipGiftRecordService.updateById(mapperFacade.map(entity,VipGiftRecord.class));
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('admin:VipGiftRecord:delete')")
    public ResponseEntity<Void> delete(@RequestBody String[]ids){
        VipGiftRecordService.removeByIds(Arrays.asList(ids));
            return ResponseEntity.ok().build();
    }


}

